package com.hex.liyue.usermodule.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.FileUtis;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.usermodule.bean.MyBankInfoResultBean;
import com.hex.liyue.usermodule.presenter.IUserBankView;
import com.hex.liyue.usermodule.presenter.UserBankPresenter;
import com.hex.liyue.usermodule.view.PhotoSelectDialog;

import java.io.File;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/15.
 * 用户银行卡界面
 */
public class UserBankActivity extends BaseActivity implements IUserBankView {
    private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
    private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
    private static final int PHOTO_REQUEST_CUT = 3;// 结果


    private int type;//1.身份证正面 2.身份证反面 3.银行卡正面 4.银行卡反面
    private File fileIdFront, fileIdBack, fileBankFront, fileBankBack;//四个文件

    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;

    @Bind(R.id.ll_withdrawCash)
    LinearLayout llWithdrawCash;
    @Bind(R.id.et_username)
    EditText etUsername;
    @Bind(R.id.et_bank_number)
    EditText etBankNumber;
    @Bind(R.id.et_bank_name)
    EditText etBankName;
    @Bind(R.id.et_withdrawCash)
    EditText etWithdrawCash;

    @Bind(R.id.iv_id_front_photo)
    ImageView ivIdFrontPhoto;
    @Bind(R.id.iv_id_back_photo)
    ImageView ivIdBackPhoto;
    @Bind(R.id.iv_bank_front_photo)
    ImageView ivBankFrontPhoto;
    @Bind(R.id.iv_bank_back_photo)
    ImageView ivBankBackPhoto;
    private UserBankPresenter userBankPresenter;
    private boolean canEdit = true;//可编辑
    private String bank_id;//银行卡信息id
    private boolean flag = false;//是否是提现
    private long balance = 0;//提现余额
    private String iDBack = "";
    private String iDPositive = "";
    private String BankBack = "";
    private String BankPositive = "";

    @Override
    protected void bindPresenter() {
        userBankPresenter = new UserBankPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        flag = getIntent().getBooleanExtra("flag", false);
        balance = getIntent().getLongExtra("balance", 0);
        userBankPresenter.getBankInfo();

        fileIdFront = new File(FileUtis.getDirectory() + File.separator + System.currentTimeMillis() + "_idFront.png");
        fileIdBack = new File(FileUtis.getDirectory() + File.separator + System.currentTimeMillis() + "_idBack.png");
        fileBankFront = new File(FileUtis.getDirectory() + File.separator + System.currentTimeMillis() + "_bankFront.png");
        fileBankBack = new File(FileUtis.getDirectory() + File.separator + System.currentTimeMillis() + "_bankBack.png");
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_user_bank;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("银行卡信息");
    }


    @OnClick({R.id.iv_toobar_back, R.id.tv_toobar_right, R.id.iv_id_front_photo, R.id.iv_id_back_photo, R.id.iv_bank_front_photo, R.id.iv_bank_back_photo})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_toobar_right:
                if (canEdit) {
                    //没有银行卡 和 认证失败 可编辑
                    userBankPresenter.savaUserBankInfo(bank_id);
                } else {
                    //认证中 和 认证通过  不可编辑
                    if (flag) {
                        //提现业务
                        userBankPresenter.withdrawCash(balance);
                    }
                }
                break;
            case R.id.iv_id_front_photo:
                //身份证正面
                type = 1;
                showPhotoDialog();
                break;
            case R.id.iv_id_back_photo:
                //身份证反面
                type = 2;
                showPhotoDialog();
                break;
            case R.id.iv_bank_front_photo:
                //银行卡正面
                type = 3;
                showPhotoDialog();
                break;
            case R.id.iv_bank_back_photo:
                //银行卡反面
                type = 4;
                showPhotoDialog();
                break;
        }
    }

    /**
     * 启动裁剪图片功能
     *
     * @param uri
     * @param size
     */
    private void startPhotoZoom(Uri uri, int size) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // crop为true是设置在开启的intent中设置显示的view可以剪裁
        intent.putExtra("crop", "true");

        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 4);
        intent.putExtra("aspectY", 3);

        // outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", size);
        intent.putExtra("outputY", size);
        intent.putExtra("return-data", false);
        if (type == 1) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileIdFront));
        } else if (type == 2) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileIdBack));
        } else if (type == 3) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileBankFront));
        } else if (type == 4) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileBankBack));
        }
        startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

    /**
     * 显示拍照/图库 对话框
     */
    public void showPhotoDialog() {
        L.d("WJ", "type=" + type);
        final PhotoSelectDialog photoSelectDialog = new PhotoSelectDialog(this);
        photoSelectDialog.setOnPhotoListener(new PhotoSelectDialog.OnPhotoListener() {
            @Override
            public void photo() {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        "image/*");
                startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
            }

            @Override
            public void camera() {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra("camerasensortype", 2);// 调用前置摄像头
                intent.putExtra("autofocus", true);// 自动对焦
                intent.putExtra("fullScreen", false);// 全屏
                intent.putExtra("showActionIcons", false);
                if (type == 1) {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileIdFront));
                } else if (type == 2) {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileIdBack));
                } else if (type == 3) {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileBankFront));
                } else if (type == 4) {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileBankBack));
                }
                startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
            }

            @Override
            public void cancel() {
                photoSelectDialog.dismiss();
            }
        });
        photoSelectDialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case PHOTO_REQUEST_TAKEPHOTO:
                    if (type == 1) {
                        startPhotoZoom(Uri.fromFile(fileIdFront), 350);
                    } else if (type == 2) {
                        startPhotoZoom(Uri.fromFile(fileIdBack), 350);
                    } else if (type == 3) {
                        startPhotoZoom(Uri.fromFile(fileBankFront), 350);
                    } else if (type == 4) {
                        startPhotoZoom(Uri.fromFile(fileBankBack), 350);
                    }
                    break;
                case PHOTO_REQUEST_GALLERY:
                    if (data != null) {
                        startPhotoZoom(data.getData(), 350);
                    }
                    break;
                case PHOTO_REQUEST_CUT:
                    if (data != null) {
                        if (type == 1) {
                            Glide.with(this).load(fileIdFront.getAbsolutePath()).centerCrop().into(ivIdFrontPhoto);
//                            ivIdFrontPhoto.setImageURI(Uri.parse(fileIdFront.getAbsolutePath()));
                        } else if (type == 2) {
//                            ivIdBackPhoto.setImageURI(Uri.parse(fileIdBack.getAbsolutePath()));
                            Glide.with(this).load(fileIdBack.getAbsolutePath()).centerCrop().into(ivIdBackPhoto);
                        } else if (type == 3) {
                            Glide.with(this).load(fileBankFront.getAbsolutePath()).centerCrop().into(ivBankFrontPhoto);
//                            ivBankFrontPhoto.setImageURI(Uri.parse(fileBankFront.getAbsolutePath()));
                        } else if (type == 4) {
//                            ivBankBackPhoto.setImageURI(Uri.parse(fileBankBack.getAbsolutePath()));
                            Glide.with(this).load(fileBankBack.getAbsolutePath()).centerCrop().into(ivBankBackPhoto);
                        }


                        break;
                    }
                    break;
            }
        }
    }

    @Override
    public String getUsername() {
        return etUsername.getText().toString();
    }

    @Override
    public String getBankNumber() {
        return etBankNumber.getText().toString();
    }

    @Override
    public String getBank() {
        return etBankName.getText().toString();
    }

    @Override
    public File getIdFront() {
        return fileIdFront;
    }

    @Override
    public File getIdBack() {
        return fileIdBack;
    }

    @Override
    public File getBankFront() {
        return fileBankFront;
    }

    @Override
    public File getBankBack() {
        return fileBankBack;
    }


    @Override
    public void setBankInfo(MyBankInfoResultBean myBankInfoResultBean) {
        if (myBankInfoResultBean.data.size() <= 0) {
            //沒有銀行卡信息
            tvToobarRight.setVisibility(View.VISIBLE);
            tvToobarRight.setText("保存");
        } else {
            MyBankInfoResultBean.DataBean dataBean = myBankInfoResultBean.data.get(0);
            if (dataBean.audit.equals("-2")) {
                //没有银行卡
                canEdit = true;
                bank_id = dataBean.bank_id;
                etUsername.setText(dataBean.username);
                etBankNumber.setText(dataBean.bank_number);
                etBankName.setText(dataBean.bank_name);
//                Glide.with(this)
//                        .load(dataBean.id_front_photo).centerCrop()
//                        .error(R.drawable.default_error_image)
//                        .into(ivIdFrontPhoto);
                iDPositive = dataBean.id_front_photo;

//                Glide.with(this)
//                        .load(dataBean.id_back_photo).centerCrop()
//                        .error(R.drawable.default_error_image)
//                        .into(ivIdBackPhoto);
                iDBack = dataBean.id_back_photo;

//                Glide.with(this)
//                        .load(dataBean.bank_front_photo).centerCrop()
//                        .error(R.drawable.default_error_image)
//                        .into(ivBankFrontPhoto);
                BankPositive = dataBean.bank_front_photo;
//                Glide.with(this)
//                        .load(dataBean.bank_back_photo).centerCrop()
//                        .error(R.drawable.default_error_image)
//                        .into(ivBankBackPhoto);
                BankBack = dataBean.bank_back_photo;
            } else if (dataBean.audit.equals("-1")) {
                //认证失败
                canEdit = true;
                bank_id = dataBean.bank_id;
                etUsername.setText(dataBean.username);
                etBankNumber.setText(dataBean.bank_number);
                etBankName.setText(dataBean.bank_name);
//                Glide.with(this)
//                        .load(dataBean.id_front_photo).centerCrop()
//                        .error(R.drawable.default_error_image)
//                        .into(ivIdFrontPhoto);
                iDPositive = dataBean.id_front_photo;

//                Glide.with(this)
//                        .load(dataBean.id_back_photo).centerCrop()
//                        .error(R.drawable.default_error_image)
//                        .into(ivIdBackPhoto);
                iDBack = dataBean.id_back_photo;

//                Glide.with(this)
//                        .load(dataBean.bank_front_photo).centerCrop()
//                        .error(R.drawable.default_error_image)
//                        .into(ivBankFrontPhoto);
                BankPositive = dataBean.bank_front_photo;
//                Glide.with(this)
//                        .load(dataBean.bank_back_photo).centerCrop()
//                        .error(R.drawable.default_error_image)
//                        .into(ivBankBackPhoto);
                BankBack = dataBean.bank_back_photo;
            } else if (dataBean.audit.equals("1")) {
                //认证中
                canEdit = false;
                bank_id = dataBean.bank_id;
                etUsername.setText(dataBean.username);
                etBankNumber.setText(dataBean.bank_number);
                etBankName.setText(dataBean.bank_name);
                Glide.with(this)
                        .load(dataBean.id_front_photo).centerCrop()
                        .error(R.drawable.default_error_image)
                        .into(ivIdFrontPhoto);
                iDPositive = dataBean.id_front_photo;

                Glide.with(this)
                        .load(dataBean.id_back_photo).centerCrop()
                        .error(R.drawable.default_error_image)
                        .into(ivIdBackPhoto);
                iDBack = dataBean.id_back_photo;

                Glide.with(this)
                        .load(dataBean.bank_front_photo).centerCrop()
                        .error(R.drawable.default_error_image)
                        .into(ivBankFrontPhoto);
                BankPositive = dataBean.bank_front_photo;
                Glide.with(this)
                        .load(dataBean.bank_back_photo).centerCrop()
                        .error(R.drawable.default_error_image)
                        .into(ivBankBackPhoto);
                BankBack = dataBean.bank_back_photo;
            } else if (dataBean.audit.equals("2")) {
                //认证通过
                canEdit = false;
                bank_id = dataBean.bank_id;
                etUsername.setText(dataBean.username);
                etBankNumber.setText(dataBean.bank_number);
                etBankName.setText(dataBean.bank_name);
            Glide.with(this)
                    .load(dataBean.id_front_photo).centerCrop()
                    .error(R.drawable.default_error_image)
                    .into(ivIdFrontPhoto);
                iDPositive = dataBean.id_front_photo;

            Glide.with(this)
                    .load(dataBean.id_back_photo).centerCrop()
                    .error(R.drawable.default_error_image)
                    .into(ivIdBackPhoto);
                iDBack = dataBean.id_back_photo;

            Glide.with(this)
                    .load(dataBean.bank_front_photo).centerCrop()
                    .error(R.drawable.default_error_image)
                    .into(ivBankFrontPhoto);
                BankPositive = dataBean.bank_front_photo;
            Glide.with(this)
                    .load(dataBean.bank_back_photo).centerCrop()
                    .error(R.drawable.default_error_image)
                    .into(ivBankBackPhoto);
                BankBack = dataBean.bank_back_photo;
            }


            if (canEdit == false) {
                //不可编辑
                tvToobarRight.setVisibility(View.GONE);
                etUsername.setEnabled(false);
                etBankNumber.setEnabled(false);
                etBankName.setEnabled(false);
                ivIdFrontPhoto.setEnabled(false);
                ivIdBackPhoto.setEnabled(false);
                ivBankFrontPhoto.setEnabled(false);
                ivBankBackPhoto.setEnabled(false);
            } else {
                //可以编辑
                tvToobarRight.setVisibility(View.VISIBLE);
                tvToobarRight.setText("保存");
            }

        }
        L.d("WJ", "能否编辑" + canEdit);
        L.d("WJ", "银行卡ID" + bank_id);
        if (flag) {
            //是提现
            tvToobarRight.setVisibility(View.VISIBLE);
            tvToobarRight.setText("确认提现");
            llWithdrawCash.setVisibility(View.VISIBLE);
            etWithdrawCash.setText("￥" + balance);
            etWithdrawCash.setEnabled(false);
        }
    }

    @Override
    public void closeCurrentActivity() {
        setResult(RESULT_OK);
        finish();

//        fileIdFront = new File(FileUtis.getDirectory() + File.separator + System.currentTimeMillis() + "_idFront.png");
//        fileIdBack = new File(FileUtis.getDirectory() + File.separator + System.currentTimeMillis() + "_idBack.png");
//        fileBankFront = new File(FileUtis.getDirectory() + File.separator + System.currentTimeMillis() + "_bankFront.png");
//        fileBankBack = new File(FileUtis.getDirectory() + File.separator + System.currentTimeMillis() + "_bankBack.png");
    }

    @Override
    public String getIdBacks() {
        return iDBack;
    }

    @Override
    public String getIdPositive() {
        return iDPositive;
    }

    @Override
    public String getBankBacks() {
        return BankBack;
    }

    @Override
    public String getBankPositive() {
        return BankPositive;
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }


}
